﻿
    //前端储存的产品集合
    var Allproducts = new Array();

    //主页全局变量，存储已加载过的产品
    var newallproducts = new Array();


    //操作区菜单js代码
    $(function () {

        $(".wrap li:first-child").hover(function () {
            $(this).addClass("on");
            $(this).parent().find(".show").css({ "display": "block" });
        });
        $(".wrap li").mouseleave(function () {
            $(this).removeClass("on");
            $(".show").css({ "display": "none" });
        });
    })

    //当页面加载完成
    $(function () {

        //初次加载数据
        SearchPro(1, 1);

        //高级搜索，隐藏&显示
        $("#btnHSearch").click(function () {
            var flag = $("#divHSearch").css("display");
            if (flag == "none") {
                $("#txtHOrderNum").val("");
                $("#txtHBeginTime").val("");
                $("#txtHEndTime").val("");
                $("#divHSearch").slideDown("slow");
            } else {
                $("#divHSearch").slideUp("slow");
            }
        });

        //点击批量管理
        $("#batchUpdate").bind("click", function () {
            //显示隐藏元素
            $(".dropdown-menu").slideToggle();
        })

        
    })


    //搜索产品
    var SearchPro = function (type, PageIndex) {
        var Searchparameter = new Object();
        Searchparameter["txtSearch"] = $("#txtSearch").val();
        Searchparameter["PageIndex"] = PageIndex;//请求页

        if (type == 1) {
            RequestProList(Searchparameter);
        }
        else {
            Searchparameter["qualityLevel"] = $("#qualityLevel").val();
            Searchparameter["category"] = $("#category").val();
            Searchparameter["gmtModifiedBegin"] = $("#gmtModifiedBegin").val();
            Searchparameter["gmtModifiedEnd"] = $("#gmtModifiedEnd").val();

            //请求产品数据集合
            RequestProList(Searchparameter);
        }
    }
    //请求数据，解析数据，异步加载要显示的产品数据列表        
    function RequestProList(Searchparameter) {

        //提示弹层
        layer.msg('正在加载数据...', { icon: 16, shade: [0.5, '＃f5f5f5'], scrollbar: false, offset: '300px', time: 50000 });
        $.ajax({
            url: "/Product/RequestProList",
            data: Searchparameter,
            success: function (data) {

                //解析产品集合    
                var objs = JSON.parse(data);
                var Currentcount = objs["Currentcount"];
                var products = objs["products"];

                //在加载产品时储存产品集合
                PushProducts(products);

                var htmls = "";
                for (var i = 0; i < products.length; i++) {

                    htmls += "<tr class='odd gradeX'>"
                    htmls += "<td>" + products[i]["offerId"] + "</td>";
                    htmls += "<td>销售中</td>";
                    htmls += "<td><img  src='" + products[i]["Proimage"] + "' style='width:40px; height:40px;'/></td>";
                    htmls += "<td><a href='" + products[i]["detailsUrl"] + "' target='_blank' >" + products[i]["subject"] + "</a></td>";
                    htmls += "<td>" + products[i]["priceRanges"] + "</td>";
                    htmls += "<td>" + products[i]["qualityLevel"] + "</td>";
                    htmls += "<td>" + products[i]["gmtLastRepost"] + "</td>";
                    htmls += "<td>" + products[i]["gmtExpire"] + "</td>";
                    htmls += " <td>hh";
                    htmls += " <a class='icon-edit' href='javascript:void(0)' onclick='Product.Add(&#39;000001&#39;)' title='编辑'>";
                    htmls += " </a>&nbsp;&nbsp;<a class='icon-remove' href='javascript:void(0)' onclick='Product.Delete(&#39;000001&#39;)' title='删除'></a>";
                    htmls += " </td>";
                    htmls += "</tr>"
                }

                //插入htnl代码
                $("#Prolist").html(htmls);

                //渲染表格
                layui.use('table', function () {
                    var table = layui.table;
                    table.init('parse-table-demo', { //转化静态表格
                        height: '550',
                        size: 'lg'                           
                    });

                    //避免屏幕闪动，在Layui脚本渲染后显示表格区域
                    $("#contentid").show();
                });

                //如果请求的是第一页，重新绑定分页
                if (Searchparameter["PageIndex"] == "1") {
                    SearchList(20, Currentcount, "Prolist");
                }
                //关闭弹层提示
                layer.closeAll();
            }
        })
    }
    //分页部分脚本。
    var SearchList = function (nums, Currentcount, tagetobjectId) {
        
        layui.use(['laypage', 'layer'], function () {
            var laypage = layui.laypage
            , layer = layui.layer;
            var isone = true;
            //完整功能
            laypage.render({
                elem: 'demo7'
                , count: Currentcount
                , limit: nums
                , limits: [10, 20, 30]
                , layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']
                , curr: function (obj) {
                isone = false;
            }()
                , jump: function (obj) {
                     
                    if (isone) {
                        //请求数据。
                        SearchPro(2,obj.curr);
                    }
                    else {
                        isone = true;
                    }
                }
            });
        });
    }



    //在加载产品时储存产品集合
    var PushProducts = function (products) {
        for (var i = 0; i < products.length; i++) {

            var on = 0
            for (var r = 0; r < Allproducts.length; r++) {

                if (Allproducts[r]["offerId"] == products[i]["offerId"]) {
                    on++;
                }
            }
            if (on == 0) {
                Allproducts.push(products[i]);
            }
            on = 0;
        }
    }

    //批量操作是把选中的产品保存到全局变量中 。
    var HandleShowPro = function (data) {
        newallproducts = new Array();
        for (var i = 0; i < data.length; i++) {

            var offerId = data[i]["offerId"];

            for (var r = 0; r < Allproducts.length ; r++) {
                if (Allproducts[r]["offerId"] == offerId) {
                    newallproducts.push(Allproducts[r]);
                }
            }
        }
    }


    //监听表格复选框选择，并且给批量操作按钮绑定事件。
    layui.use('table', function () {
        var table = layui.table;
        //监听表格复选框选择
        table.on('checkbox(parse-table-demo)', function (obj) {
            //console.log(obj)
        });
       
        //绑定事件给所有的批量操作按钮
        $(' .a_btn_click').on('click', function () {

            //获取选中的要操作的产品
            var checkStatus = table.checkStatus('idTest')
            var data = checkStatus.data;

            //把选中的产品保存到全局变量中 。
            HandleShowPro(data);

            //获取当前批量操作类型
            var type = $(this).attr('data-type');

            //弹出批量操作修改方式选择框
            SelectOperationMode(type);

        });
    });


    //弹出批量操作修改方式选择框
    var layerIndex = 0;
    var SelectOperationMode = function (OperationType) {

        // 1 批量操作类型 
        // 2 操作对象类型
        // 3 如果是操作对象2 传递参数。

        var htmls = "<div class='Update-but'>";
        htmls += "<button class='layui-btn layui-btn-warm layui-btn-small selectclick' dataType='CurrentProsUpdate')>当前选中的产品修改</button>";
        
        //处理是修改属性时显示按钮样式
        if (OperationType != "EditProductFeature") {
            htmls += "<button class='layui-btn layui-btn-small selectclick' dataType='CustomCategoryUpdate'>按自定义分类修改</button>";
            htmls += "<button class='layui-btn layui-btn-small layui-btn-normal  selectclick' dataType='AllProsUpdate'>按全店产品修改</button>";
        }
        else {         
            htmls += "<button class='layui-btn layui-btn-small selectclick' dataType='CustomCategoryUpdate'>按产品类目筛选修改</button>";
            htmls += "<button class='layui-btn layui-btn-small layui-btn-normal layui-btn-disabled' dataType='AllProsUpdate'>按全店产品修改</button>";
        }
        htmls += "</div> ";

        layerIndex = layer.open({
            type: 1,
            skin: 'demo1-class',
            title: "批量操作修改方式",
            area: ['600px', '110px'], //宽高
            content: htmls,
            success: function () {
                //绑定选择操方式事件
                $(".selectclick").on("click", function () {
                    var OperationObjType = $(this).attr("dataType");
                    selectclick(OperationType, OperationObjType)
                })
            }
        });
    }

    //给子窗体使用的参数。
    var ToMDIChildParameter = new Object();
    
    // 选中事件
    var selectclick = function (OperationType,OperationObjType)
    {
        //关闭上一个弹出框。
        layer.close(layerIndex);

        //操作对象类型为《按当前选中的产品修改》
        if (OperationObjType == "CurrentProsUpdate")
        {
            if (newallproducts.length < 1)
            {
                layer.alert("请选中要修改的产品");
                return false;
            }
            if (OperationType == "EditProductFeature")
            {
                newallproducts

                var map = {},
                dest = [];
                for (var i = 0; i < newallproducts.length; i++) {
                    var ai = newallproducts[i];
                    if (!map[ai.postCategryId]) {
                        dest.push({
                            postCategryId: ai.postCategryId,
                            name: ai.name,
                            data: [ai]
                        });
                        map[ai.postCategryId] = ai;
                    } else {
                        for (var j = 0; j < dest.length; j++) {
                            var dj = dest[j];
                            if (dj.postCategryId == ai.postCategryId) {
                                dj.data.push(ai);
                                break;
                            }
                        }
                    }
                }

                if (dest.length > 1)
                {
                    layer.alert("所选的产品不属于同一类目");
                    return false;
                }

            }
        }
        //操作对象类型为《按自定义类型修改》
        if (OperationObjType == "CustomCategoryUpdate") {
            var htmls = "";
            htmls += " <div class='Update-but'><button class='layui-btn layui-btn-small layui-btn-normal' onclick='SubmitUpdate()'>确认修改</button></div> ";
            layerIndex = layer.open({
                type: 1,
                skin: 'demo1-class',
                title: "自定义产品分类",
                area: ['400px', '200px'], //宽高
                content: htmls
            });
        }

        var parameter1 = "";
        var parameter2 = "";

        //参数对象
        ToMDIChildParameter = { OperationType: OperationType, OperationObjType: OperationObjType, parameter1: parameter1, parameter2: parameter2 };

        //弹出批量修改产品窗体
        EjectupdateProforms(OperationType);

    }

    //弹出批量修改产品窗体
    var EjectupdateProforms = function (OperationType) {
        var showName = $("a[data-type=" + OperationType + "]").text();
        layer.open({    
            type: 2,
            skin: 'demo1-class',
            resize:false,
            title: "修改" + showName,
            area: ['820px', '530px'], //宽高
            content: '/ProductBatchEdit/Index?OperationType=' + OperationType + '',
            success: function () {
            }
        });
    }
